package algorithm;

import java.util.Arrays;

/**
 * @Description: 选择排序
 * @Author: hmm
 * @CreateDate: 2019/8/27
 * @UpdateUser:
 * @UpdateDate: 2019/8/27
 * @UpdateRemark: 修改备注
 * @Version: 1.0
 * 时间复杂度 O(n^2)  空间复杂度O(1)
 */
public class SelectionSort {
    public static int[] selectionSort(int[] array) {
        if (array.length == 0) {
            return array;
        }
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                minIndex = array[j] < array[minIndex] ? j : minIndex;
            }
            swap(array, i, minIndex);
        }
        return array;
    }

    private static void swap(int[] array, int i, int minIndex) {
        int temp = array[minIndex];
        array[minIndex] = array[i];
        array[i] = temp;
    }

    public static void main(String[] args) {
        int[] arr = {1, 6, 2, 41, 11, 25, 8, 31, 25, 22};
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
